package com.lanyam.travel.base.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lanyam.travel.base.entity.Role;
import com.lanyam.travel.base.entity.RoleMenu;
import com.lanyam.travel.base.entity.RolePermission;
import com.lanyam.travel.base.entity.TicketType;
import com.lanyam.travel.base.service.IRolePermissionService;
import com.lanyam.travel.base.service.IRoleService;
import com.lanyam.travel.base.service.ITicketTypeService;
import com.lanyam.travel.base.utils.JSONResult;

@RestController
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private IRoleService roleService;

	/**
	 * 增加角色，同步新增角色中拥有的权限
	 * @param role
	 * @return
	 */
	@PostMapping("/addRole")
	public JSONResult addRole(@RequestBody Role role)
	{
		if(null == role || StringUtils.isEmpty(role.getDescription()))
		{
			return JSONResult.errorMsg("参数不全");
		}
		String roleId = UUID.randomUUID().toString().replace("-", "");
		role.setId(roleId);
		role.setPower(20);
		role.setCreateDate(new Date());
		List<Integer> permissionIds = role.getPermissionIds();
		if(null != permissionIds && permissionIds.size() > 0)
		{
			RolePermission rolePermission;
			List<RolePermission> list = new ArrayList<RolePermission>();
			for (Integer permissionId : permissionIds)
			{
				rolePermission = new RolePermission();
				rolePermission.setRoleId(roleId);;
				rolePermission.setPermissionId(permissionId);
				list.add(rolePermission);
			}
			roleService.batchInsertRolePermission(list);
		}
		
		List<String> menuIds = role.getMenuIds();
		if(null != menuIds && menuIds.size() > 0)
		{
			RoleMenu roleMenu;
			List<RoleMenu> list = new ArrayList<RoleMenu>();
			for (String menuId : menuIds)
			{
				roleMenu = new RoleMenu();
				roleMenu.setRoleId(roleId);;
				roleMenu.setMenuId(menuId);
				list.add(roleMenu);
			}
			roleService.batchInsertRoleMenu(list);
		}
		
		roleService.addRole(role);
		return JSONResult.ok();
	}
	
	/**
	 * 获取所有角色
	 * @return
	 */
	@GetMapping("/getAllRole")
	public JSONResult getAllRole()
	{
		List<Role> allRole = roleService.getAllRole();
		return JSONResult.ok(allRole);
	}
	
	/**
	 * 更新角色信息,包含角色里面的权限同步更新
	 * @param role
	 * @return
	 */
	@PostMapping("/updateRole")
	public JSONResult updateRole(@RequestBody Role role)
	{
		if(null == role)
		{
			return JSONResult.errorMsg("参数不全");
		}
		roleService.update(role);
		return JSONResult.ok(role);
	}
	
	/**
	 * 删除角色，同步删除角色的权限
	 * @param id
	 * @return
	 */
	@PostMapping("/deleteRole")
	public JSONResult deleteRole(String id)
	{
		if(StringUtils.isEmpty(id))
		{
			return JSONResult.errorMsg("参数不全");
		}
		roleService.deleteRole(id);
		return JSONResult.ok();
	}
	
	/**
	 * 获取角色详情，同步获取角色中的权限
	 * @param id
	 * @return
	 */
	@PostMapping("/getRoleById")
	public JSONResult getRoleById(String id)
	{
		if(StringUtils.isEmpty(id))
		{
			return JSONResult.errorMsg(id);
		}
		Role role = roleService.getRoleById(id);
		return JSONResult.ok(role);
	}
	/**
	 * @param role
	 * @return
	 */
	@PostMapping("/saveRolePermission")
	public JSONResult saveRolePermission(@RequestBody Role role)
	{
		String roleId = role.getId();
		//处理角色对应的权限
		List<Integer> permissionIds = role.getPermissionIds();
		if(StringUtils.isEmpty(roleId)|| null == permissionIds)
		{
			return JSONResult.errorMsg("参数不全");
		}
		List<Integer> ids = new ArrayList<Integer>();
		for (Integer string : permissionIds) {
			ids.add(string);
		}
		roleService.saveRolePermission(roleId, permissionIds);
		//处理角色显示的菜单
		List<String> menuIds = role.getMenuIds();
		roleService.saveRoleMenu(roleId, menuIds);
		
		return JSONResult.ok();
	}
}
